#include "GameScene.h"
#include "Item.h"
#include "Background.h"
#include "time.h"
SceneManager& SceneManager::instance(){
	static SceneManager sm;
	return sm;

}

void GameScene::update(){
	//update background
	for (std::vector<Background*>::iterator it=m_pBackground.begin();it!=m_pBackground.end();it++)
	{
		(*it)->render_pos_.m_Texcoord[1]=(*it)->length_*current_/scene_length_;
		(*it)->Render();
	}
	//	printf("Curr:%d,%d\n",current_,m_pCharacter->render_pos_.m_Position[1]);
	//update character
	if (m_pCharacter->render_pos_.m_Position[1]<200)
	{
		int a=(200-m_pCharacter->render_pos_.m_Position[1])*0.08;
		if(a<3) a=3;
		m_pCharacter->render_pos_.m_Position[1]+=a;
		current_-=a;

	}
	m_pCharacter->global_pos_.y+=m_pCharacter->m_sVelocity.y;
	//m_pCharacter->render_pos_.m_Position[0]=m_pCharacter->global_pos_.x;
	m_pCharacter->render_pos_.m_Position[1]=m_pCharacter->global_pos_.y-current_;
	//	printf("Char:%d,%d\n",m_pCharacter->render_pos_.m_Position[1],current_);
	m_pCharacter->Render();
	//update clouds
	for (std::vector<Cloud*>::iterator it=m_pClouds.begin();it!=m_pClouds.end();it++)
	{
		if ((*it)->global_pos_.y<current_-720)
			continue;
		if ((*it)->global_pos_.y>current_+720)
			continue;
		(*it)->render_pos_.m_Position[0]=(*it)->global_pos_.x;
		(*it)->render_pos_.m_Position[1]=(*it)->global_pos_.y-current_;
		if(m_pCharacter->Touch(**it))
			if(m_pCharacter->m_sVelocity.y>0)
			{
				m_pCharacter->m_sVelocity.y=-15;
				//printf("Touched\n");
			}
	//	printf("Cloud: %d\n",(*it)->render_pos_.m_Position[1]);
		(*it)->Render();
	}

	//update items
	for (std::vector<Item*>::iterator it=m_pItems.begin();it!=m_pItems.end();it++)
	{
		if ((*it)->global_pos_.y<current_-720)
			continue;
		if ((*it)->global_pos_.y>current_+720)
			continue;
		if (!(*it)->visible)
			continue;
		(*it)->render_pos_.m_Position[1]=(*it)->global_pos_.y-current_;
		
		(*it)->Render();
		if (m_pCharacter->Touch(**it))
		{
			printf("Touched\n");
			switch((*it)->type_){
			case Item::ITEM_RAINBOW_WING:
				m_pCharacter->m_sVelocity.y=-25;
				break;
			case Item::ITEM_GOLDEN_WING:
				m_pCharacter->m_sVelocity.y=-20;
				break;
			case Item::ITEM_WHITE_WING:
				m_pCharacter->m_sVelocity.y=-15;
				break;
			}
			(*it)->visible=false;
		}
	}





}


void GameScene::clear(){
	m_pClouds.clear();
	m_pItems.clear();
	m_pBackground.clear();
	
}


GameScene::GameScene(){
	clear();
	init();
}
GameScene::~GameScene(){
	clear();

};

void GameScene::init()
{
	///Initialize all
	current_=50000;
	scene_length_=50000;
	

	///Initialize background
	Background* pBg=new Background();
	pBg->SetPosition(new Vertex_VT(0,0,0,0,0));
	pBg->stride_x=480;
	pBg->stride_y=720;
	pBg->length_=0;
	pBg->speed_=0;
	m_pBackground.push_back(pBg);
	pBg=new Background;
	pBg->SetPosition(new Vertex_VT(0,0,0,961,0));
	pBg->stride_x=480;
	pBg->stride_y=720;
	pBg->length_=720;
	pBg->speed_=1;
	m_pBackground.push_back(pBg);
	pBg=new Background;
	pBg->SetPosition(new Vertex_VT(0,0,0,481,0));
	pBg->stride_x=480;
	pBg->stride_y=720;
	pBg->length_=1328;
	pBg->speed_=2;
	m_pBackground.push_back(pBg);

	//Clouds initialize
	Cloud* pCl= new Cloud(Cloud::C_T3);
	pCl->SetPosition(200,scene_length_);
	
	m_pClouds.push_back(pCl);
	pCl= new Cloud(Cloud::C_T1);
	pCl->SetPosition(100,scene_length_);
	m_pClouds.push_back(pCl);

	srand(time(NULL));
	
	int temp_length=scene_length_+1000;
	while (temp_length>100)
	{
		int i=rand()%3;
		pCl=new Cloud((Cloud::CloudType)i);
		temp_length-=rand()%250+1;
		pCl->SetPosition(rand()%350+1,temp_length);
		m_pClouds.push_back(pCl);
	}
	//Items initialize

	Item* pIt;
	temp_length=scene_length_;
	while(temp_length>200)
	{
		int i=rand()%3;
			pIt=new Item((Item::ItemType)i);
		temp_length-=(rand()%2000+100);
		pIt->SetPosition(rand()%350+1,temp_length);
		
		m_pItems.push_back(pIt);
	}



	//Character initialize
	Character *pCh=new Character;
	pCh->SetPosition(200,scene_length_+400);
	pCh->render_pos_.m_Position[1]=pCh->global_pos_.y-current_;
	pCh->render_pos_.m_Position[0]=pCh->global_pos_.x;
	pCh->stride_x=128;
	pCh->stride_y=128;
	//pCh->m_sVelocity.y=0;
	
	m_pCharacter=pCh;
}